import React, {PureComponent} from 'react';
import {connect} from 'dva';
import {
    Form, Input, Button, Card, Select
} from 'antd';
import {message} from "antd/lib/index";
const FormItem = Form.Item;

@connect(state => ({
    rule: state.goodsTypeEdit
}))
@Form.create()
export default class GoodsTypeEdit extends PureComponent {

    handleParentModalVisible = (e) => {
        this.props.handleModalVisible();
    }

    handleSubmit = (editType,cat_id,e) => {
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                let user_id = '';
                const userInfoState = sessionStorage.getItem("userInfoState");
                if (userInfoState !== null) {
                    const {userInfo} = JSON.parse(userInfoState);
                    user_id = userInfo.user_id;
                }
                this.props.dispatch({
                    type: `goodsTypeEdit/${editType}`,
                    payload: {...values,cat_id: cat_id,user_id:user_id},
                    callback: (response) => {
                        if (response.result === 0) {
                            message.success('提交成功');
                            this.props.handleModalVisible();
                            this.props.handleModalSuccess(this.props.pagination);
                        } else {
                            message.success('提交失败');
                        }
                    }
                });
            }
        });
    }

    render() {
        const {submitting, record, editType, typeSList} = this.props;
        const {getFieldDecorator, getFieldValue} = this.props.form;
        const formInitValue = editType === 'add' ? '' : record;
        const cat_id = editType === 'add' ? '' : record.cat_id;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 7},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 12},
                md: {span: 10},
            },
        };
        const submitFormLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 10, offset: 7},
            },
        };

        return (
            <Card bordered={false}>
                <Form
                    onSubmit={this.handleSubmit}
                    hideRequiredMark
                >
                    <FormItem
                        {...formItemLayout}
                        label="商品类型名称"
                    >
                        {getFieldDecorator('cat_name', {
                            initialValue:  formInitValue.cat_name || '',
                            rules: [{
                                required: true, message: '请输入商品类型名称',
                            }],
                        })(
                            <Input placeholder="商品类型名称"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="类型状态"
                    >
                        {getFieldDecorator('enabled', {
                            initialValue: formInitValue !== '' ? ((formInitValue.enabled ? "1" : "0") || '') :'',
                        })(
                            <Select placeholder="请选择" style={{width: '100%'}} >
                                <Option value="">请选择</Option>
                                <Option value="0">不可用</Option>
                                <Option value="1">可用</Option>
                            </Select>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="商品属性分组"
                    >
                        {getFieldDecorator('attr_group', {
                            initialValue: formInitValue !== '' ? ((formInitValue.attr_group==="pro_param" ? "pro_param" : "pro_attr") || '') :'',
                        })(
                            <Select placeholder="请选择" style={{width: '100%'}} >
                                <Option value="">请选择</Option>
                                <Option value="pro_param">参数</Option>
                                <Option value="pro_attr">属性</Option>
                            </Select>
                        )}
                    </FormItem>
                    <FormItem {...submitFormLayout} style={{marginTop: 32}}>
                        <Button type="primary" onClick={this.handleSubmit.bind(this,editType,cat_id)}>
                            提交
                        </Button>
                        <Button style={{marginLeft: 8}} onClick={this.handleParentModalVisible}>取消</Button>
                    </FormItem>
                </Form>
            </Card>
        );
    }
}
